var Global = require('global');

cc.Class({
    extends: cc.Component,

    properties: {
    	scorePanel:{
            type:cc.Layout,
            default:null,
        },
		high:{
            type:cc.Label,
            default:null,
        },
        score:{
            type:cc.Label,
            default:null,
        },
        speed:{
            type:cc.Label,
            default:null,
        },
        scoreFinal:{
            type:cc.Label,
            default:null,
        },
        restartPanel:{
        	type:cc.Layout,
        	default:null,
        },
    	jumpDuration:1,
        jumpHeight:20,
		oldSpeed:0,
    },
    onLoad () {
    	//开启物理引擎
    	cc.director.getPhysicsManager().enabled = true;

    	//事件绑定
    	this.node.parent.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);

    	//获取刚体
        this.rigidBody = this.node.getComponent(cc.RigidBody);
        //获取飞翔动作
        this.flyAnimation = this.node.getComponent(cc.Animation);

        //跳跃
        this.jumpAction = this.createJumpAction();

        //鸟和计分牌面板在最上层
        this.node.zIndex = 8;
    	this.scorePanel.node.zIndex = 9;
    	this.restartPanel.node.zIndex = 9;

    	//按钮面板不可见
    	this.restartPanel.node.active = false;

    	this.init();
    },
    start () {
    	this.rigidBody.active = false;
    },
    init(){
    	cc.log("初始化");
    	//初始化游戏状态
    	Global.isStart = false;
    	Global.isOver = false;
		Global.high = localStorage.getItem('high') || 0;
    	Global.scores = 0;
    	Global.speed = 1;
    	//初始化记分板
		this.high.node.getComponent(cc.Label).string=Global.high;
        this.score.node.getComponent(cc.Label).string=0;
    	this.speed.node.getComponent(cc.Label).string=1;
    	//初始化鸟的位置
    	this.node.x = -18;
    	this.node.y = 25;
    	//初始化鸟的重力
    	this.rigidBody.linearVelocity = cc.v2(0,0);
        this.rigidBody.active = false;
        //隐藏重新开始面板
        this.restartPanel.node.active = false;
        //播放鸟飞翔动画
        this.flyAnimation.play();	
    },
    onCollisionEnter:function(other,self){
    	//碰上柱子
    	if(other.node.group=='upAndDown'){
        	cc.log('碰上了，game over');
        	//修改游戏状态
        	Global.isOver = true;
        	//设置最终分数
        	this.scoreFinal.node.getComponent(cc.Label).string=Global.scores;
			Global.scores>Global.high && (Global.high = Global.scores);
			localStorage.setItem('high',Global.high);
        	//停止播放鸟飞翔动画
        	this.flyAnimation.stop();
        	//显示重新开始面板
        	this.restartPanel.node.active = true;
        //碰上加分物体
    	}else if(other.node.group=='score'){
    		cc.log('得分:'+(++Global.scores));
    		//修改记分板
    		this.score.node.getComponent(cc.Label).string=Global.scores;
        }else if(other.node.group=='quick'){
            cc.log('速度+1,当前速度:'+(++Global.speed));
            //移除加速物体
            other.node.removeFromParent();
            this.speed.node.getComponent(cc.Label).string=Global.speed;
    	}
    },
    createJumpAction(){
        // 跳跃上升
        var jumpUp = cc.moveBy(this.jumpDuration, cc.v2(0, this.jumpHeight)).easing(cc.easeCubicActionOut());
        // 动作回调
        return cc.sequence(jumpUp, cc.callFunc(function(){
            this.rigidBody.active = true;
        },this));
    },
    onTouchStart(event){
        //cc.log("跳跃");
        if(!Global.isOver){
        	//修改游戏状态
        	Global.isStart = true;
        	//跳跃时去掉重力
	        this.rigidBody.linearVelocity = cc.v2(0,0);
	        this.rigidBody.active = false;
	        //播放跳跃动作
	        this.node.runAction(this.jumpAction);
        }
    },
    //重新开始
    buttonRestartClick(){
    	//删除所有柱子
    	var upAndDownList = this.node.parent.getComponent("start").upAndDownList;
    	for(var i =0;i<upAndDownList.length;i++){
    		upAndDownList[i].removeFromParent();
    	}
    	//删除加速物体
    	var quick = this.node.parent.getComponent("start")._quick;
    	quick&&quick.removeFromParent();
    	quick = null;

    	//初始化操作
    	this.init();
    }

});